package bg.cinema_system.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bg.cinema_system.global.ConnectionJDBC;

public class HallDAO {

	public static int getHallNumber(int hall_id) throws SQLException {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class HallDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT halls.number_hall FROM cinema.halls " 
		+ "WHERE halls.hall_id = ?";

		PreparedStatement ps = null;
		ResultSet rs =  null;
		int hall_number = -1;
		try {
			ps = dbConnection.prepareStatement(sqlStatement);
			ps.setInt(1, hall_id);
	
			// Get customer data from database
			rs = ps.executeQuery();
			if (rs.next()) {
				hall_number = rs.getInt("number_hall");
		}
		} catch (SQLException e) {
			throw new SQLException(e);
		} finally {
			try {
				ps.close();
				rs.close();
				dbConnection.close();
			} catch (SQLException e) {
				throw new SQLException(e);
			}
		}
		return hall_number;
	}
	
	public static List<String> getHallNumbers() throws SQLException{
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class HallDAO: Can't get database connection");
		}

		String sql = "SELECT halls.number_hall FROM cinema.halls halls";
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<String> hallNumbersList = new ArrayList<String>();
		try {
			ps= dbConnection.prepareStatement(sql);
			// Get customer data from database
			rs = ps.executeQuery();
			while (rs.next()) {
				hallNumbersList.add(Integer.toString(rs.getInt("number_hall"))) ;
			}
		} catch (SQLException e) {
			throw new SQLException(e);
		} finally {
			try {
				ps.close();
				rs.close();
				dbConnection.close();
			} catch (SQLException e) {
				throw new SQLException(e);
			}
		}
		return hallNumbersList;
	}

}
